/*
 Copyright (c) 2009, Ivan Popivanov
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.

 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.

 * Neither the name of StockCandles nor the names of its contributors
 may be used to endorse or promote products derived from this software
 without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef OHLC_H
#define OHLC_H

#include <math.h>

#include <QVarLengthArray>
#include <QDate>

#include "TimeFrame.h"

typedef QVarLengthArray<double> DoubleArray;
typedef QVarLengthArray<uint> UintArray;
typedef QVarLengthArray<QDate> DateArray;

class Ohlc
{
public:
   QString s;			// symbol
   DateArray d;			// date
   DoubleArray o;		// open
   DoubleArray h;		// high
   DoubleArray l;		// low
   DoubleArray c;		// close
   DoubleArray &vals;	// for a single-value series
   UintArray v;			// volume

   Ohlc() : vals(o) {}
   Ohlc(const QString &symbol) : vals(o), s(symbol) {}

   void init(const QString &symbol) {s = symbol; resize(0);}

   void reserve(int newSize) {d.reserve(newSize); o.reserve(newSize); h.reserve(newSize); l.reserve(newSize); c.reserve(newSize); v.reserve(newSize);}
   void resize(int newSize) {d.resize(newSize); o.resize(newSize); h.resize(newSize); l.resize(newSize); c.resize(newSize); v.resize(newSize);}
   void clear() {d.clear(); o.clear(); h.clear(); l.clear(); c.clear(); v.clear();}
   int size() const {return d.size();}
   void append(const QDate &date, double open, double hi, double low, double close, uint volume)
   {
      d.append(date); o.append(open); h.append(hi); l.append(low); c.append(close); v.append(volume);
   }

   // Set the elements at index i with the values of the series o at index j
   void setAt(int i, const Ohlc &other, int j)
   {
      d[i] = other.d[j]; o[i] = other.o[j]; h[i] = other.h[j]; l[i] = other.l[j]; c[i] = other.c[j]; v[i] = other.v[j];
   }
   void setAt(int i, const Ohlc &other) {setAt(i, other, i);}
   void setAt(int i, const QDate &date, double open, double hi, double low, double close, uint volume)
   {
      d[i] = date; o[i] = open; h[i] = hi; l[i] = low; c[i] = close; v[i] = volume;
   }

   bool isRedAt(int i) const {return o[i] > c[i];}
   bool isGreenAt(int i) const {return o[i] <= c[i];}

   bool fromDaily(const Ohlc &other, const TimeFrame &timeFrame);

   int sample(const Ohlc &other, int first, const TimeFrame &tf, int maxCnt);

   bool findHighLow(const QDate &startDate, const TimeFrame &period, double &hi, double &low);

   // Given the latest date - finds the beginning of a time period (TimeFrame*times)
   int periodEnd(int latest, const TimeFrame &period, int times = 1) const;

   int first() const {return 0;}
   int last() const {return size()-1;}
   int latest() const {return size()-1;}

   void copy(const Ohlc &o);

   bool isIdValid(int i) {return i >= first() && i <= latest();}

   bool loadFromYahooCsv(const QString &path, const QDate *start = NULL);
   bool storeToYahooCsv(const QString &path);

   int dateToIndex(const QDate &date);

private:
   double round(double d) { return floor(d*10000) / 10000; }

   void reverse();
};

#endif // OHLCARRAYS_H